import 'package:flutter/material.dart';
import 'package:jm_flutter_common/jm_flutter_common.dart';
import '../../../../common/utils/show.dart';
import '../../../ui.dart';
import 'log_item.dart';

class OverlayLogWidget extends StatefulWidget {
  final VoidCallback? onClosed;
  final VoidCallback? onGotoDebugPage;
  final VoidCallback? onSizeChanged;
  final VoidCallback? onMinSize;
  final ValueChanged<bool>? onShowTimeChanged;
  final ValueChanged<LogLevel>? onLogLevelChanged;
  final bool showTime;
  final LogLevel? logLevel;

  const OverlayLogWidget({
    Key? key,
    this.onClosed,
    this.onShowTimeChanged,
    required this.showTime,
    this.onGotoDebugPage,
    this.onSizeChanged,
    this.onMinSize,
    this.logLevel,
    this.onLogLevelChanged,
  }) : super(key: key);

  @override
  State<OverlayLogWidget> createState() => _OverlayLogWidgetState();
}

class _OverlayLogWidgetState extends State<OverlayLogWidget> {
  final ScrollController scrollController = ScrollController();

  bool showTime = false;
  LogLevel logLevel = LogLevel.verbose;

  @override
  void initState() {
    //logd("OverlayLogWidget", "initState");
    showTime = widget.showTime;
    logLevel = widget.logLevel ?? LogLevel.verbose;
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(top: 6, bottom: 6, left: 4, right: 4),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            height: 30,
            alignment: Alignment.centerRight,
            child: ListView(
              shrinkWrap: true,
              //reverse: true,
              padding: EdgeInsets.zero,
              scrollDirection: Axis.horizontal,
              //mainAxisAlignment: MainAxisAlignment.end,
              children: [
                MyIconButton(
                  icon: const MySvg(
                    "assets/svg/filter.svg",
                    package: "jm_flutter",
                    color: Colors.white,
                    width: 19,
                  ),
                  size: 30,
                  onPressed: () {
                    //logLevel = LogLevel.values[index];
                    logLevel = logLevel.index == LogLevel.values.length - 1
                        ? LogLevel.values[0]
                        : LogLevel.values[logLevel.index + 1];
                    setState(() {});
                    widget.onLogLevelChanged?.call(logLevel);
                    Show.toast("Log Level: ${logLevel.name}");
                  },
                ),
                MyIconButton(
                    size: 30,
                    icon: const MySvg(
                      "assets/svg/zoom.svg",
                      package: "jm_flutter",
                      color: Colors.white,
                      width: 18,
                    ),
                    onPressed: () {
                      widget.onSizeChanged?.call();
                    }),
                MyIconButton(
                    size: 30,
                    icon: const MySvg(
                      "assets/svg/window_expan.svg",
                      package: "jm_flutter",
                      color: Colors.white,
                      width: 18,
                    ),
                    onPressed: () {
                      widget.onGotoDebugPage?.call();
                    }),
                MyIconButton(
                    size: 30,
                    icon: const Icon(
                      CupIcons.time,
                      color: Colors.white,
                      size: 20,
                    ),
                    onPressed: () {
                      showTime = !showTime;
                      setState(() {});
                      widget.onShowTimeChanged?.call(showTime);
                    }),
                MyIconButton(
                    size: 30,
                    icon: const Icon(
                      CupIcons.delete,
                      color: Colors.white,
                      size: 20,
                    ),
                    onPressed: () {
                      setState(() {
                        JmLog.clear();
                      });
                    }),
                MyIconButton(
                  size: 30,
                  icon: const Icon(
                    CupIcons.arrow_down_to_line,
                    color: Colors.white,
                    size: 20,
                  ),
                  onPressed: () {
                    scrollController.animateTo(0,
                        duration: const Duration(milliseconds: 200),
                        curve: Curves.ease);
                  },
                ),
                MyIconButton(
                  size: 30,
                  icon: const Icon(
                    CupIcons.minus_circle,
                    color: Colors.white,
                    size: 20,
                  ),
                  onPressed: () {
                    widget.onMinSize?.call();
                  },
                ),
                MyIconButton(
                  size: 30,
                  icon: const Icon(
                    CupIcons.clear_circled,
                    color: Colors.white,
                    size: 20,
                  ),
                  onPressed: () {
                    widget.onClosed?.call();
                  },
                ),
              ],
            ),
          ),
          Expanded(
            child: StreamBuilder<bool>(
                stream: JmLog.logStream,
                builder: (context, snapshot) {
                  return Container(
                    alignment: Alignment.topCenter,
                    child: ListView.builder(
                      shrinkWrap: true,
                      padding: EdgeInsets.zero,
                      reverse: true,
                      itemBuilder: (context, index) {
                        final log =
                            JmLog.logList[JmLog.logList.length - 1 - index];
                        if (log.level.index > logLevel.index) {
                          return const SizedBox();
                        }
                        return JmLogDescItem(
                          logDesc: log,
                          showTime: showTime,
                        );
                      },
                      itemCount: JmLog.logList.length,
                      controller: scrollController,
                      physics: const BouncingScrollPhysics(),
                    ),
                  );
                }),
          ),
        ],
      ),
    );
  }
}
